//Created by Roman Clarkson for Jaws.Core.Web
//Original work Copyright(C) 2008.  All Rights Reserved.

using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Jaws.Core.Web.Data;

namespace Jaws.Core.Web.Entities.Resources
{
    public class Resource : IXmlSerializable, IEntityBase
    {
        public Resource()
        {
            Id = Guid.NewGuid();
        }

        public string Type { get; set; }
        public string CultureCode { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }

        #region Implementation of IXmlSerializable

        /// <summary>
        /// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute" /> to the class.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema" /> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)" /> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)" /> method.
        /// </returns>
        public XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="T:System.Xml.XmlReader" /> stream from which the object is deserialized. 
        /// </param>
        public void ReadXml(XmlReader reader)
        {
            Type = reader["type"];
            CultureCode = reader["culturecode"];
            Key = reader["key"];
            Value = reader["value"];
            Id = new Guid(reader["id"]);
        }

        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">
        /// The <see cref="T:System.Xml.XmlWriter" /> stream to which the object is serialized. 
        /// </param>
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("resource");
            writer.WriteAttributeString("id", Id.ToString());
            writer.WriteAttributeString("type", Type);
            writer.WriteAttributeString("key", Key);
            writer.WriteAttributeString("value", Value);
            writer.WriteAttributeString("culturecode", CultureCode);
            writer.WriteEndElement();
        }

        #endregion

        #region IEntityBase Members

        public Guid Id { get; set; }

        #endregion
    }
}